package com.acce.gxsc.page.registerCar.editCompanyInfo;

import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.CompanyBean;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.NetCompanyBean;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by huxley on 2017/5/26.
 */

public class EditCompanyInfoPresenter extends BasePresenter<EditCompanyInfoView> {

    public void loadCompanyList() {
        WorkModel.getInstance().loadCompanyList().map(new Func1<ResultBean<NetCompanyBean>, List<CompanyBean>>() {
            @Override
            public List<CompanyBean> call(ResultBean<NetCompanyBean> result) {
                for (int i = 0; i < result.result.fatherCompany.size(); i++) {
                    CompanyBean goodsBean = result.result.fatherCompany.get(i);
                    goodsBean.sonList = new ArrayList<>();
                    for (int j = 0; j < result.result.sonCompany.size(); j++) {
                        CompanyBean child = result.result.sonCompany.get(j);
                        if (child.fatherId.equals(goodsBean.companyId)) {
                            goodsBean.sonList.add(child);
                        }
                    }
                }
                return result.result.fatherCompany;
            }
        }).subscribe(new Subscriber<List<CompanyBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    LogUtils.logout("1111111");
                    getView().showProgressDialog("加载中...", false);
                }
            }
            @Override
            public void onNext(List<CompanyBean> datas) {
                if (mViewRef != null) {
                    LogUtils.logout("2222222");
                    getView().loadCompanyListSuccess(datas);
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    LogUtils.logout("3333333");
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    LogUtils.logout("4444444");
                    e.printStackTrace();
                    getView().hideProgressDialog();
                }
            }
        });
    }
}
